<template>
  <div class="app-container">
    <!-- <el-row>
      <h1 style="color: #303133;">汇总统计</h1>
      <el-row :gutter="40" class="panel-group">
        <el-col :xs="12" :sm="12" :lg="8" class="card-panel-col">
          <div class="card-panel" @click="handleSetLineChartData('newVisitis')">
            <div class="card-panel-icon-wrapper icon-money">
              <svg-icon icon-class="money" class-name="card-panel-icon" />
            </div>
            <div class="card-panel-description">
              <div class="card-panel-text">
                总消耗
              </div>
              <count-to :start-val="0" :end-val="sumItem.allConsume" :duration="2600" class="card-panel-num" />
            </div>
          </div>
        </el-col>
        <el-col :xs="12" :sm="12" :lg="8" class="card-panel-col">
          <div class="card-panel" @click="handleSetLineChartData('newVisitis')">
            <div class="card-panel-icon-wrapper icon-money">
              <svg-icon icon-class="money" class-name="card-panel-icon" />
            </div>
            <div class="card-panel-description">
              <div class="card-panel-text">
                总公司收益
              </div>
              <count-to :start-val="0" :end-val="sumItem.allCamExam" :duration="2600" class="card-panel-num" />
            </div>
          </div>
        </el-col>
        <el-col :xs="12" :sm="12" :lg="8" class="card-panel-col">
          <div class="card-panel" @click="handleSetLineChartData('newVisitis')">
            <div class="card-panel-icon-wrapper icon-money">
              <svg-icon icon-class="money" class-name="card-panel-icon" />
            </div>
            <div class="card-panel-description">
              <div class="card-panel-text">
                总个人收益
              </div>
              <count-to :start-val="0" :end-val="sumItem.allPerExam" :duration="2600" class="card-panel-num" />
            </div>
          </div>
        </el-col>
        <el-col :xs="12" :sm="12" :lg="8" class="card-panel-col">
          <div class="card-panel" @click="handleSetLineChartData('newVisitis')">
            <div class="card-panel-icon-wrapper icon-money">
              <svg-icon icon-class="money" class-name="card-panel-icon" />
            </div>
            <div class="card-panel-description">
              <div class="card-panel-text">
                日消耗
              </div>
              <count-to :start-val="0" :end-val="sumItem.nowConsume" :duration="2600" class="card-panel-num" />
            </div>
          </div>
        </el-col>
        <el-col :xs="12" :sm="12" :lg="8" class="card-panel-col">
          <div class="card-panel" @click="handleSetLineChartData('newVisitis')">
            <div class="card-panel-icon-wrapper icon-money">
              <svg-icon icon-class="money" class-name="card-panel-icon" />
            </div>
            <div class="card-panel-description">
              <div class="card-panel-text">
                日公司收益
              </div>
              <count-to :start-val="0" :end-val="sumItem.nowCamExam" :duration="2600" class="card-panel-num" />
            </div>
          </div>
        </el-col>
        <el-col :xs="12" :sm="12" :lg="8" class="card-panel-col">
          <div class="card-panel" @click="handleSetLineChartData('newVisitis')">
            <div class="card-panel-icon-wrapper icon-money">
              <svg-icon icon-class="money" class-name="card-panel-icon" />
            </div>
            <div class="card-panel-description">
              <div class="card-panel-text">
                日个人收益
              </div>
              <count-to :start-val="0" :end-val="sumItem.nowPerExam" :duration="2600" class="card-panel-num" />
            </div>
          </div>
        </el-col>
      </el-row>
    </el-row> -->
    <div style="height: 30px;"></div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="投手" prop="uid">
        <el-select v-model="queryParams.uid" placeholder="请选择投手" clearable>
          <el-option
            v-for="dict in peoList"
            :key="dict.userId"
            :label="dict.userName"
            :value="dict.userId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="mode">
        <el-select v-model="queryParams.mode" placeholder="请选择类型" clearable>
          <el-option
            v-for="dict in dict.type.xb_mode"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="日期" prop="time">
        <el-date-picker clearable
          v-model="queryParams.time"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="项目" prop="projectId">
        <el-select v-model="queryParams.projectId" placeholder="请选择项目" clearable>
          <el-option
            v-for="dict in proList"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="售价" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入售价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="消耗" prop="consume">
        <el-input
          v-model="queryParams.consume"
          placeholder="请输入消耗"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="进粉数" prop="num">
        <el-input
          v-model="queryParams.num"
          placeholder="请输入进粉数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单个成本" prop="cost">
        <el-input
          v-model="queryParams.cost"
          placeholder="请输入单个成本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司收益" prop="campanyEarn">
        <el-input
          v-model="queryParams.campanyEarn"
          placeholder="请输入公司收益"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="个人收益" prop="personalEarn">
        <el-input
          v-model="queryParams.personalEarn"
          placeholder="请输入个人收益"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:consumeRecords:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:consumeRecords:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:consumeRecords:remove']"
        >删除</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:consumeRecords:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="consumeRecordsList" @selection-change="handleSelectionChange" :default-sort="{prop: 'date', order: 'descending'}">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="投手" align="center" prop="userName" />
      <el-table-column label="类型" align="center" prop="mode" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.xb_mode" :value="scope.row.mode"/>
        </template>
      </el-table-column>
      <el-table-column label="日期" align="center" prop="time" width="180" sortable>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目" align="center" prop="proName" />
      <el-table-column label="售价" align="center" prop="price" />
      <el-table-column label="消耗" align="center" prop="consume" />
      <el-table-column label="进粉数" align="center" prop="num" />
      <el-table-column label="单个成本" align="center" prop="cost" />
      <el-table-column label="消耗截图" align="center" prop="consumeImgs" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.consumeImgs" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="公司收益" align="center" prop="campanyEarn" />
      <el-table-column label="个人收益" align="center" prop="personalEarn" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:consumeRecords:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:consumeRecords:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改消耗记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="用户ID" prop="uid">
          <el-input v-model="form.uid" placeholder="请输入用户ID" />
        </el-form-item> -->
        <el-form-item label="类型" prop="mode">
          <el-radio-group v-model="form.mode">
            <el-radio
              v-for="dict in dict.type.xb_mode"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="日期" prop="time">
          <el-date-picker clearable
            v-model="form.time"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="项目" prop="projectId">
          <el-select v-model="form.projectId" placeholder="请选择项目" clearable>
            <el-option
              v-for="dict in proList"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="售价" prop="price">
          <el-input v-model="form.price" placeholder="请输入售价" />
        </el-form-item>
        <el-form-item label="消耗" prop="consume">
          <el-input v-model="form.consume" placeholder="请输入消耗" />
        </el-form-item>
        <el-form-item label="进粉数" prop="num">
          <el-input v-model="form.num" placeholder="请输入进粉数" />
        </el-form-item>
        <!-- <el-form-item label="单个成本" prop="cost">
          <el-input v-model="form.cost" placeholder="请输入单个成本" />
        </el-form-item> -->
        <el-form-item label="消耗截图" prop="consumeImgs">
          <image-upload v-model="form.consumeImgs"/>
        </el-form-item>
        <!-- <el-form-item label="公司收益" prop="campanyEarn">
          <el-input v-model="form.campanyEarn" placeholder="请输入公司收益" />
        </el-form-item>
        <el-form-item label="个人收益" prop="personalEarn">
          <el-input v-model="form.personalEarn" placeholder="请输入个人收益" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConsumeRecords,listConsumeRecordsAdmin, getConsumeRecords, delConsumeRecords, addConsumeRecords, updateConsumeRecords,getProList,summary } from "@/api/app/consumeRecords";
import { getPeoples } from "@/api/app/accountRecords";
import CountTo from 'vue-count-to'
export default {
  name: "ConsumeRecords",
  dicts:['xb_mode'],
  props: {
    value: [String, Object, Array],
    // 图片数量限制
    limit: {
      type: Number,
      default: 0,
    },
    // 大小限制(MB)
    fileSize: {
      type: Number,
      default: 5,
    },
    // 文件类型, 例如['png', 'jpg', 'jpeg']
    fileType: {
      type: Array,
      default: () => ["png", "jpg", "jpeg"],
    },
    // 是否显示提示
    isShowTip: {
      type: Boolean,
      default: true
    }
  },
  components:{
    CountTo
  },
  watch: {
    limit: {
      deep: true,
      handler(val) {
        console.log(val);
        this.queryParams.projectId = val
        this.wid = val;
        this.getList();

      }
    },
  },
  data() {
    return {
      wid:null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 消耗记录表格数据
      consumeRecordsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        uid: null,
        mode:null,
        time: null,
        projectId: null,
        price: null,
        consume: null,
        num: null,
        cost: null,
        consumeImgs: null,
        campanyEarn: null,
        personalEarn: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        mode: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        time: [
          { required: true, message: "日期不能为空", trigger: "blur" }
        ],
        projectId: [
          { required: true, message: "项目ID不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "售价不能为空", trigger: "blur" }
        ],
        consume: [
          { required: true, message: "消耗不能为空", trigger: "blur" }
        ],
        num: [
          { required: true, message: "进粉数不能为空", trigger: "blur" }
        ],
        cost: [
          { required: true, message: "单个成本不能为空", trigger: "blur" }
        ],
        consumeImgs: [
          { required: true, message: "消耗截图不能为空", trigger: "blur" }
        ],
        campanyEarn: [
          { required: true, message: "公司收益不能为空", trigger: "blur" }
        ],
        personalEarn: [
          { required: true, message: "个人收益不能为空", trigger: "blur" }
        ],
      },
      proList:[],peoList:[],sumItem:{}
    };
  },
  mounted(){
    this.getList();
    this.getPros();
    this.getPeo();
    summary(0).then(res=>{
      this.sumItem = res.data
    })
  },
  methods: {
    //查询投手
    getPeo(){
      getPeoples().then(res=>{
        this.peoList = res.data;
      })
    },
    //项目列表
    getPros(){
      getProList().then(res=>{
        this.proList = res.data;
      })
    },
    /** 查询消耗记录列表 */
    getList() {
      this.loading = true;
      listConsumeRecordsAdmin(this.queryParams).then(response => {
        this.consumeRecordsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        uid: null,
        mode:1,
        time: null,
        projectId: null,
        price: null,
        consume: null,
        num: null,
        cost: null,
        consumeImgs: null,
        campanyEarn: null,
        personalEarn: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加消耗记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getConsumeRecords(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改消耗记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateConsumeRecords(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConsumeRecords(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除消耗记录编号为"' + ids + '"的数据项？').then(function() {
        return delConsumeRecords(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/consumeRecords/export', {
        ...this.queryParams
      }, `consumeRecords_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.panel-group {
  margin-top: 18px;

  .card-panel-col {
    margin-bottom: 32px;
  }

  .card-panel {
    height: 108px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
    border-color: rgba(0, 0, 0, .05);

    &:hover {
      .card-panel-icon-wrapper {
        color: #fff;
      }

      .icon-people {
        background: #40c9c6;
      }

      .icon-message {
        background: #36a3f7;
      }

      .icon-money {
        background: #f4516c;
      }

      .icon-shopping {
        background: #34bfa3
      }
    }

    .icon-people {
      color: #40c9c6;
    }

    .icon-message {
      color: #36a3f7;
    }

    .icon-money {
      color: #f4516c;
    }

    .icon-shopping {
      color: #34bfa3
    }

    .card-panel-icon-wrapper {
      float: left;
      margin: 14px 0 0 14px;
      padding: 16px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }

    .card-panel-icon {
      float: left;
      font-size: 48px;
    }

    .card-panel-description {
      float: right;
      font-weight: bold;
      margin: 26px;
      margin-left: 0px;

      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }

      .card-panel-num {
        font-size: 20px;
        color: #F56C6C;
      }
    }
  }
}

@media (max-width:550px) {
  .card-panel-description {
    display: none;
  }

  .card-panel-icon-wrapper {
    float: none !important;
    width: 100%;
    height: 100%;
    margin: 0 !important;
    .svg-icon {
      display: block;
      margin: 14px auto !important;
      float: none !important;
    }
  }
}
.tom-box{
  box-sizing: border-box;
  padding: 20px;
  box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
  height: 720px;
}
.table-box{
  box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
  box-sizing: border-box;
  padding: 20px;
  height: 720px;
}
</style>
